package com.example.sixthweek;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.List;

public class RateListActivity extends AppCompatActivity {
private Handler handler;
public String logDate="";
public final String DATE_SP_KEY = "lastRateDateStr";
private static final String TAG = "List";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //setContentView(R.layout.activity_rate_list);
        SharedPreferences sp = getSharedPreferences("myrate", Context.MODE_PRIVATE);
        logDate = sp.getString(DATE_SP_KEY,"");
        Log.i(TAG, "lastRateDateStr=" + logDate);
        ListView myList = findViewById(R.id.mylistview);
        String[] list_data = {"one", "two", "three", "four"};
        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_data);
        myList.setAdapter(adapter);

        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {

                //收到数据
                if(msg.what==5){
                    List<String> list2 = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(RateListActivity.this, android.R.layout.simple_list_item_1, list2);
                    myList.setAdapter(adapter);

                }
                super.handleMessage(msg);
            }
        };
        MyTask myTask = new MyTask();
        myTask.setHandler(handler);//将数据传给handler

        Thread t = new Thread(myTask);
        t.start();//自动调用当前对象myTask的run方法


    }
}